<template>
    <view class="cn-user-asset-log">
        <drawer :scrolltolower="scrolltolower" :drawer_show.sync="drawer_show">
            <template #main>
                <custom><template #backText>资产日志</template></custom>
                <userAssetLogTimeline ref="userAssetLogTimeline" :parentFormtag.sync="data.formtag" :parentForm.sync="data.form">
                    <template #top>
                        <nButtonTopRightFloat title="筛选日志" :tap="()=>{drawer_show = true}"></nButtonTopRightFloat>
                    </template>
                </userAssetLogTimeline>
            </template>
            <template #left>
                <nFormItem type="date_to_date" title="选择时间" :data.sync="data" field="start_created_date" field2="end_created_date" text_lr="right"></nFormItem>
                <nBar title="资产变动" subtitle="(多选)" :is_solid_bottom="true"></nBar>
                <nGrid ref="nGrid" :can_select_item="true" :data.sync="data" :row="grid_change_user_asset.row" :col="grid_change_user_asset.col"
                    :border="grid_change_user_asset.border" :list="grid_change_user_asset.list"></nGrid>
                <nBar title="行为类型" subtitle="(多选)" :is_solid_bottom="true"></nBar>
                <view class="cu-list menu">
                    <nMenuItemRow1 class_title="text-df" :title="name" :tap="()=>{selectLogTypes(key)}" v-for="(name, key) in $store.state.user.user_asset_log_types"
                        :key="key" :class="data.form.action_types | stringInArray(key) ? 'bg-nddyny-force' : ''">
                    </nMenuItemRow1>
                </view>
                <foot key="foot3" v-if="!objectIsEq(data.form, data.formbak)" :content="[
                    {
                        'title': '重置',
                        'tap': resetForm,
                        'class': 'bg-nddyny light basis-xs'
                    },
                    {
                        'title': '重置并刷新',
                        'tap': resetRefreshForm,
                        'class': 'bg-deep-nddyny basis-sm'
                    },
                    {
                        'title': '筛选日志',
                        'tap': submit,
                        'class': 'bg-nddyny basis-sm'
                    }
                ]">
                </foot>
                <foot key="foot1" v-else-if="!objectIsEq(data.form, data.formTrue)" :tap="resetRefreshForm" title="重置并刷新">
                </foot>
            </template>
        </drawer>
    </view>
</template>

<script>
    import userAssetLogTimeline from '@/components/nddyny/userAssetLogTimeline.vue';
    export default {
        components: {
            userAssetLogTimeline,
        },
        data() {
            let grid_change_user_asset = this.$store.state.user.is_seller_firm == 1 ?
                this.$user.grid.change_seller_user_asset :
                this.$user.grid.change_user_asset;
            let form = {
                start_created_date: '',
                end_created_date: '',
                action_types: ''
            };
            Object.keys(grid_change_user_asset.list).forEach(key => {
                form['is_change_' + key] = 0
            });
            return {
                drawer_show: false,
                grid_change_user_asset: grid_change_user_asset,
                data: {
                    formtag: {},
                    form: this.$nddyny.object.copy(form),
                    formbak: this.$nddyny.object.copy(form),
                    formTrue: this.$nddyny.object.copy(form),
                }
            }
        },
        onLoad(params) {
            if (params.action_types) {
                this.data.form.action_types = params.action_types;
            }
            if (params.action_ids) {
                this.data.form.action_ids = params.action_ids;
            }
        },
        methods: {
            scrolltolower() {
                this.$refs.userAssetLogTimeline.getList(this.$api.list_action_add);
            },
            selectLogTypes(key) {
                this.data.form.action_types = this.$nddyny.string.array.change(this.data.form.action_types, key);
            },
            resetForm() {
                this.$refs.nGrid.clearSelectItemKeyList();
                this.data.form = this.$nddyny.object.copy(this.data.formbak);
            },
            resetRefreshForm() {
                this.resetForm();
                this.$nextTick(() => {
                    this.submit();
                })
            },
            submit() {
                if (this.data.form.start_created_date != '' &&
                    this.data.form.end_created_date != '' &&
                    this.data.form.start_created_date*1 > this.data.form.end_created_date*1) {
                    let true_start_created_date = this.data.form.end_created_date;
                    this.data.form.end_created_date = this.data.form.start_created_date;
                    this.data.form.start_created_date = true_start_created_date;
                }
                this.$refs.userAssetLogTimeline.getList(this.$api.list_action_force_init, () => {
                    this.drawer_show = false;
                });
                this.data.formTrue = this.$nddyny.object.copy(this.data.form);
            },
            objectIsEq(obj1, obj2) {
                return this.$nddyny.object.isEq(obj1, obj2);
            },
        },
        watch: {
            drawer_show(to, from) {
                if (to == false && !this.$nddyny.object.isEq(this.data.form, this.data.formTrue)) {
                    this.data.form = this.$nddyny.object.copy(this.data.formTrue);
                }
            }
        }
    }
</script>
